import {Component, Input, OnInit} from '@angular/core';
import {FormBuilder, FormGroup} from "@angular/forms";
import {MaintenanceService} from "../../shared/service/maintenance.service";
import {NzModalSubject} from "ng-zorro-antd";
declare var $:any;

@Component({
  selector: 'app-maintain-search',
  templateUrl: './maintain-search.component.html',
  styleUrls: ['./maintain-search.component.css']
})
export class MaintainSearchComponent implements OnInit {
  //获取父页面传过来的值
  // _name: string;
  // @Input()
  // set name(value: string) {
  //   this._name = value;
  // }
  startPick1:any;
  endPick1:any;
  highForm:FormGroup;
  types:any;
  statuses:any = [{
    "id":0,
    "name":"维护中"
  },{
    "id":1,
    "name":"已结束"
  }];

  constructor(private fb:FormBuilder,
              private maintenanceService:MaintenanceService,
              private subject: NzModalSubject
  ) {
    this.subject.on('onDestory', () => {
      // console.log('destroy');
    });
  }

  ngOnInit() {
    // console.log(this._name);
    this.createForm();
    this.getTypes();
    //初始化开始时间
    this.startPick1 = $('#startDate1').datetimepicker({
      // sideBySide: true, //可以同时选择日期和时间
      format: 'YYYY-MM-DD HH:mm',
      locale: 'zh-cn',
      ignoreReadonly: true,
      stepping: 30,        //设置最小间隔分钟数
      useCurrent: false,
    });
    this.startPick1.on("dp.change", (e) => {
      //限制结束时间
      $('#endDate1').data("DateTimePicker").minDate(e.date);
    });
    //初始化结束时间
    this.endPick1 = $('#endDate1').datetimepicker({
      // sideBySide: true, //可以同时选择日期和时间
      format: 'YYYY-MM-DD HH:mm',
      locale: 'zh-cn',
      ignoreReadonly: true,
      stepping: 30,         //设置最小间隔分钟数
      useCurrent: false,
    });
    this.endPick1.on("dp.change", (e) => {
      //限制开始时间
      $('#startDate1').data("DateTimePicker").maxDate(e.date);
    });
  }

  //初始化页面表单
  createForm(){
    this.highForm = this.fb.group({
      maintainType1:'',
      maintainStatus:'',
      starttime1:'',
      endtime1:''
    });
  }

  //清空选择条件
  clear(){
    this.highForm.patchValue({
      maintainType1:'',
      maintainStatus:'',
      starttime1:'',
      endtime1:''
    });
  }

  //获取类型
  getTypes(){
    this.maintenanceService.getMaintenanceTypes().then(
      res=>{
        this.types = res;
      }
    );
  }

  //关闭当前modal
  handleCancel(e) {
    this.subject.destroy('onCancel');
  }

  //查询
  detailedSearch(){
    this.highForm.patchValue({
      starttime1:$('#starttime1').val(),
      endtime1:$('#endtime1').val()
    });
    //向父页面传值
    this.subject.next(this.highForm.value);
    this.subject.destroy('onCancel');
  }
}
